package com.hengda.museumonline.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.RegisterBean;
import com.hengda.museumonline.http.CallServer;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/9/23 13:49
 * @E-mail：jeffy12138@126.com
 */
public class GuestbookActivity extends BaseActivity implements View.OnClickListener {
    private android.widget.EditText msg;
    private android.widget.EditText mobile;
    private android.widget.LinearLayout submit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guestbook);
        this.submit = (LinearLayout) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        this.mobile = (EditText) findViewById(R.id.mobile);
        this.msg = (EditText) findViewById(R.id.msg);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:
                sunmit();
                break;
        }
    }

    private void sunmit() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "guestbook/index", RequestMethod.POST);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("token", spUtil.getToken());
        request.add("msg", String.valueOf(msg.getText()));
        request.add("mobile", String.valueOf(mobile.getText()));
        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
                dialog.show();
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    RegisterBean registerBean = gson.fromJson(response.get().toString(), RegisterBean.class);
                    if (1 == registerBean.getStatus()) {
                        getToast("提交成功");
                    } else if (0 == registerBean.getStatus()) {
                        getToast(registerBean.getMsg());
                    }
                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("responseCode:" + responseCode + ",Exception:" + exception);
            }

            @Override
            public void onFinish(int what) {
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CallServer.getRequestInstance().cancelAll();
    }
}
